package cn.tedu.day01.jnsdlfaq.dept.controller;

import cn.tedu.day01.jnsdlfaq.dept.pojo.SysDept;
import cn.tedu.day01.jnsdlfaq.dept.service.SysDeptService;
import cn.tedu.day01.jnsdlfaq.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 部门controller
 *
 * @author duwq
 */
@RestController
@RequestMapping("/dept/")
@CrossOrigin(origins = "*", maxAge = 3600)
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 查询所有部门
     *
     * @return
     */
    @RequestMapping("list")
    public AjaxResult selectAll() {
        List<SysDept> depts = sysDeptService.selectAll();
        return AjaxResult.success(depts);
    }

    /**
     * 添加部门
     *
     * @param dept
     * @return
     */
    @RequestMapping("add")
    public AjaxResult addDept(SysDept dept) {
        int rows = sysDeptService.addDept(dept);
        System.out.println(rows);
        return AjaxResult.success(rows);
    }

    /**
     * 更新部门
     *
     * @param dept
     * @return
     */
    @RequestMapping("update")
    public AjaxResult updateDept(SysDept dept) {
        int rows = sysDeptService.updateDept(dept);
        System.out.println(rows);
        return AjaxResult.success(rows);
    }

    /**
     * 根据ID删除部门
     *
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public AjaxResult deleteById(String id) {
        System.out.println(id + "======");
        int rows = sysDeptService.deleteById(Integer.parseInt(id));
        System.out.println("删除成功!");
        return AjaxResult.success(rows);
    }

    /**
     * 根据ID查询部门
     *
     * @param id
     * @return
     */
    @RequestMapping("selectById")
    public AjaxResult selectById(String id) {
        SysDept dept = sysDeptService.selectById(Integer.parseInt(id));
        System.out.println(dept);
        return AjaxResult.success(dept);
    }


}
